﻿using Microsoft.AspNetCore.Mvc;
using OpenIddict.Abstractions;
using System.Xml.Linq;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace YDT_AuthServices.Controllers
{
    /// <summary>
    /// 作用域控制器
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class ScopeController : ControllerBase
    {
        public readonly IOpenIddictScopeManager _ScopeManager;

        public ScopeController(IOpenIddictScopeManager scopeManager)
        {
            _ScopeManager = scopeManager;
        }


        // GET: api/<ScopeController>
        [HttpGet]
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/<ScopeController>/5
        [HttpGet("{id}")]
        public string Get(int id)
        {
            return "value";
        }

        // 1、添加作用域
        [HttpPost]
        public async Task CreateScopeAsync(string Name, string DisplayName)
        {
            // 1、添加
            if (await _ScopeManager.FindByNameAsync(Name) == null)
            {
                await _ScopeManager.CreateAsync(new OpenIddictScopeDescriptor()
                {
                    Name = Name, // 作用域【商品管理】
                    DisplayName = DisplayName,//【商品管理】
                    // 资源指的就是微服务
                    Resources =
                    {
                        "ProductService", // 微服务名称
                        "OrderService",
                        "PaymentService",
                        "SeckillService"
                    }
                });
            }
        }

        // PUT api/<ScopeController>/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/<ScopeController>/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
        }
    }
}
